package com.taikang.hmp.frame.common.chat;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import net.sf.json.JSONObject;

import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.WsOutbound;

import com.taikang.hmp.frame.common.HDConstant;

public class WebSocketMessageInbound extends MessageInbound {

	private final String user;// 当前连接的用户名称
	private final String type;// 当前连接的用户类型

	public WebSocketMessageInbound(String user, String type) {
		this.user = user;
		this.type = type;
	}

	public String getUser() {
		return this.user;
	}

	public String getType() {
		return type;
	}

	/**
	 * 建立连接的触发的事件
	 * 
	 * @author 高焕杰
	 */
	@Override
	protected void onOpen(WsOutbound outbound) {
		// 触发连接事件，在连接池中添加连接
		JSONObject result = new JSONObject();
		result.element("type", "user_join");
		result.element("user", this.user);
		result.element("username", WebSocketMessageInboundPool.chatuser
				.get(this.user));
		System.out.println(this.user+"onlineuser:"+WebSocketMessageInboundPool.chatuser
				.get(this.user));
		WebSocketMessageInboundPool.sendMessageToAllUser(result.toString());// 向所有在线用户推送当前用户上线的消息
		WebSocketMessageInboundPool.addMessageInbound(this);// 向连接池添加当前的连接对象
		
		if (HDConstant.CHAT_TYPE_HEALTHER.equals(this.type)) {// 如果是健康管理师，则发送对应的客户列表
		result = new JSONObject();
		result.element("type", "get_online_user");
		result.element("list", WebSocketMessageInboundPool.healthuser.get(this.user));
		
		WebSocketMessageInboundPool.sendMessageToUser(this.user, result
				.toString());	// 向当前连接发送对应管理用户的列表
		}

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -100);
		String startdate = format.format(cal.getTime());// 默认获取最近一个月的历史消息
		// 向当前连接发送当前在线用户的历史消息
		this.sendHistoryMsg(this.user, startdate,HDConstant.CHAT_STATE_UNREAD);
		if (HDConstant.CHAT_TYPE_USER.equals(this.type)) {// 如果是用户，则发送对应的健康管理师信息
			sendHealther(this.user);
		}
	}

	@Override
	protected void onClose(int status) {
		WebSocketMessageInboundPool.removeMessageInbound(this);// 触发关闭事件，在连接池中移除连接
		JSONObject result = new JSONObject();
		result.element("type", "user_leave");
		result.element("user", this.user);
		result.element("username", WebSocketMessageInboundPool.chatuser
				.get(this.user));
		WebSocketMessageInboundPool.sendMessageToAllUser(result.toString());// 向在线用户发送当前用户退出的消息
	}

	@Override
	protected void onBinaryMessage(ByteBuffer message) throws IOException {
		throw new UnsupportedOperationException("Binary message not supported.");
	}

	/**
	 * 客户端发送消息到服务器时触发事件
	 * 
	 * @author zhaocs
	 */
	@Override
	protected void onTextMessage(CharBuffer message) throws IOException {
		JSONObject obj = JSONObject.fromObject(message.toString());
		String mtype = obj.getString("type");
		String fromid = obj.getString("from");
		String toid = obj.getString("to");
		if (mtype.equals("message")) {
			
			obj.element("fromid", fromid);
			obj.element("toid", toid);
			obj.put("from", WebSocketMessageInboundPool.chatuser.get(fromid));
			obj.put("to", WebSocketMessageInboundPool.chatuser.get(toid));
			SimpleDateFormat format = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			Calendar cal = Calendar.getInstance();
			cal.setTimeInMillis(Long.parseLong(obj.getString("timestamp")));
			obj.element("timestamp", format.format(cal.getTime()));

			WebSocketMessageInboundPool.sendMessageToUser(
					obj.getString("toid"), obj.toString());
			WebSocketMessageInboundPool.sendMessageToUser(obj
					.getString("fromid"), obj.toString());
			WebSocketMessageInboundPool.saveMsg(obj);
		} else if (mtype.equals("getallhistory")) {
			sendHistoryMsg(fromid,"2000-01-01 00:00:00","1");

		}
		System.out.println("--------:" + obj.toString());
		// WebSocketMessageInboundPool.sendMessageToAllUser(message.toString());//向所有在线用户发送消息

	}

	// 发送用户的健康管理师信息
	private void sendHealther(String userid) {
		JSONObject result = new JSONObject();
		result.element("type", "get_healther");
		result.element("list", WebSocketMessageInboundPool
				.getHealtherMsg(userid));
		WebSocketMessageInboundPool.sendMessageToUser(this.user, result
				.toString());// 向当前连接发送当前在线用户的健康管理师
	}

	// 发送历史消息
	private void sendHistoryMsg(String userid, String startdate,String state) {
		JSONObject result = new JSONObject();
		result.element("type", "get_history_msg");
		result.element("list", WebSocketMessageInboundPool.getHistoryMsg(
				userid, startdate,state));
		WebSocketMessageInboundPool.sendMessageToUser(this.user, result
				.toString());// 向当前连接发送当前在线用户的健康管理师
	}
}